﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Linq;

namespace Gmantis.Controls.DataGrid
{
    public class DataGridRowsPanel : RecyclingPanel
    {
        // Fields
        private double MaxRowWidth;

        // Methods
        public DataGridRowsPanel()
        {
            this.SetCustomDefaultValues();
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            this.View.ScrollManager.Viewport = finalSize;
            foreach (DataGridRow row in this.VisibleRows.Items)
            {
                foreach (DataGridColumn column in this.VisibleColumns.Items)
                {
                    if (row.HasCellPresenter(column))
                    {
                        DataGridCellPresenter presenter = this.View.DataGrid[row, column].Presenter;
                        if (presenter != null)
                        {
                            presenter.MergedRangeCandidate = new DataGridCellsRange(presenter.Cell);
                        }
                    }
                }
            }
            if (this.View.DataGrid.MergingCellsDelegate != null)
            {
                DataGridMergingCellsEventArgs e = new DataGridMergingCellsEventArgs
                {
                    Viewport = this.View.Viewport
                };
                this.View.DataGrid.MergingCellsDelegate(this.View.DataGrid, e);
                e.Viewport = null;
            }
            foreach (DataGridRow row2 in this.VisibleRows.Items)
            {
                foreach (DataGridColumn column2 in this.VisibleColumns.Items)
                {
                    if (row2.HasCellPresenter(column2))
                    {
                        DataGridCellPresenter presenter2 = this.View.DataGrid[row2, column2].Presenter;
                        if (presenter2 != null)
                        {
                            if (presenter2.MergedRangeCandidate != presenter2.MergedRange)
                            {
                                presenter2.MergedRange = presenter2.MergedRangeCandidate;
                            }
                            presenter2.MergedRangeCandidate = null;
                        }
                    }
                }
            }
            this.RecalculateColumnsWidths();
            this.UpdateHorizontalOffset();
            this.UpdateVerticalOffset();
            if (this.View.UpdateScrollBars())
            {
                return new Size();
            }
            double num = (((this.View.GetRenderLeadingColumnWidth() + this.View.ScrollManager.FrozenLeftWidth) + this.View.ScrollManager.FrozenRightWidth) + this.View.Model.VerticalFreezingSeparatorWidth) + this.View.ScrollManager.ExtentWidth;
            num = Math.Min(finalSize.Width, num);
            double frozenTopHeight = this.View.ScrollManager.FrozenTopHeight;
            double bottomOffset = finalSize.Height - this.View.ScrollManager.FrozenBottomHeight;
            Math.Min((finalSize.Height - this.View.ScrollManager.FrozenTopHeight) - this.View.ScrollManager.FrozenBottomHeight, this.View.ScrollManager.ExtentHeight);
            foreach (UIElement element in base.Children)
            {
                if (element is DataGridRowPresenter)
                {
                    DataGridRowPresenter presenter3 = (DataGridRowPresenter)element;
                    ViewRow viewRow = presenter3.ViewRow;
                    if (viewRow != null)
                    {
                        Canvas.SetZIndex(presenter3, this.VisibleRows.FirstVisibleItem - viewRow.Index);
                        double renderHeight = viewRow.RenderHeight;
                        presenter3.InvalidateCellsArrange();
                        presenter3.Arrange(new Rect(0.0, viewRow.Offset, num, renderHeight));
                        if (!viewRow.Row.IsFrozen)
                        {
                            double num5 = renderHeight;
                            if (presenter3._elementCells != null)
                            {
                                foreach (UIElement element2 in presenter3._elementCells.Children)
                                {
                                    if (element2 is DataGridCellPresenter)
                                    {
                                        num5 = Math.Max(num5, ((DataGridCellPresenter)element2).ActualHeight);
                                    }
                                }
                            }
                            base.ClipPresenterHorizontally(presenter3, num, num5, frozenTopHeight, bottomOffset, viewRow.Offset);
                        }
                        else
                        {
                            presenter3.Clip = null;
                        }
                    }
                }
                else if (element is DataGridDetailsPresenter)
                {
                    DataGridDetailsPresenter presenter4 = (DataGridDetailsPresenter)element;
                    ViewRow row4 = presenter4.ViewRow;
                    if (row4 != null)
                    {
                        double detailsRenderHeight = row4.DetailsRenderHeight;
                        if (!row4.Row.IsFrozen)
                        {
                            base.ClipPresenterHorizontally(presenter4, num, detailsRenderHeight, frozenTopHeight, bottomOffset, row4.DetailsOffset);
                        }
                        else
                        {
                            presenter4.Clip = null;
                        }
                        double renderLeadingColumnWidth = this.View.GetRenderLeadingColumnWidth();
                        presenter4.Arrange(new Rect(renderLeadingColumnWidth, row4.DetailsOffset, Math.Max((double)0.0, (double)(num - renderLeadingColumnWidth)), detailsRenderHeight));
                    }
                }
            }
            return finalSize;
        }

        internal bool CheckViewPortIsFullHorizontally(double accumulatedWidth)
        {
            if (accumulatedWidth < this.View.ScrollManager.AvailableSize.Width)
            {
                if ((this.ValidFillParameters.HorizontalDirection == GeneratorDirection.Forward) && ((this.ValidFillParameters.ColumnIndex > this.View.Model.FrozenColumnCount) || (this.ValidFillParameters.ColumnOffset < 0.0)))
                {
                    this.View.ScrollManager.ScrollHorizontally(this.View.Model.Columns.Count - 1, 0.0, GeneratorDirection.Backward);
                    return false;
                }
                if ((this.ValidFillParameters.HorizontalDirection == GeneratorDirection.Backward) && ((this.ValidFillParameters.ColumnIndex < (this.View.Model.Columns.Count - 1)) || (this.ValidFillParameters.ColumnOffset < 0.0)))
                {
                    this.View.ScrollManager.ScrollHorizontally(0, 0.0, GeneratorDirection.Forward);
                    return false;
                }
            }
            return true;
        }

        internal bool CheckViewPortIsFullVertically(double accumulatedHeight)
        {
            if (accumulatedHeight < this.View.ScrollManager.AvailableSize.Height)
            {
                if ((this.ValidFillParameters.VerticalDirection == GeneratorDirection.Forward) && ((this.ValidFillParameters.RowIndex > this.View.Model.FrozenTopRowsCount) || (this.ValidFillParameters.RowOffset < 0.0)))
                {
                    this.View.ScrollManager.ScrollVertically(this.View.Model.Rows.Count - 1, 0.0, GeneratorDirection.Backward);
                    return false;
                }
                if ((this.ValidFillParameters.VerticalDirection == GeneratorDirection.Backward) && ((this.ValidFillParameters.RowIndex < ((this.View.Model.Rows.Count - 1) - this.View.Model.FrozenBottomRowsCount)) || (this.ValidFillParameters.RowOffset < 0.0)))
                {
                    this.View.ScrollManager.ScrollVertically(0, 0.0, GeneratorDirection.Forward);
                    return false;
                }
            }
            return true;
        }

        private double GetRenderWidthOfShownColumns(int firstVisibleColumn, int lastVisibleColumn, bool includeFrozenColumns)
        {
            double num = 0.0;
            foreach (DataGridColumn column in this.GetShownColumns(firstVisibleColumn, lastVisibleColumn, includeFrozenColumns))
            {
                num += column.ViewColumn.RenderWidth;
            }
            return num;
        }

        private IEnumerable<RowPart> GetRowPartsInOrder(ViewRow viewRow, GeneratorDirection generatorDirection)
        {
            if (!viewRow.Row.IsTopFrozen && ((generatorDirection != GeneratorDirection.Forward) || viewRow.Row.IsFrozen))
            {
                yield return RowPart.Detail;
                yield return RowPart.Row;
            }
            else
            {
                yield return RowPart.Row;
                yield return RowPart.Detail;
            }
        }

        internal IEnumerable<DataGridColumn> GetShownColumns(int firstVisibleColumn, int lastVisibleColumn, bool includeFrozenColumns)
        {
            if (includeFrozenColumns)
            {
                for (int i = 0; i < this.View.Model.FrozenColumnCount; i++)
                {
                    DataGridColumn iteratorVariable1 = this.View.Model.Columns.OrderedColumns[i];
                    if (iteratorVariable1.Visibility == Visibility.Visible)
                    {
                        yield return iteratorVariable1;
                    }
                }
            }
            if ((firstVisibleColumn != -1) && (lastVisibleColumn != -1))
            {
                for (int j = firstVisibleColumn; j <= lastVisibleColumn; j++)
                {
                    DataGridColumn iteratorVariable3 = this.View.Model.Columns.OrderedColumns[j];
                    if (iteratorVariable3.Visibility == Visibility.Visible)
                    {
                        yield return iteratorVariable3;
                    }
                }
            }
        }

        public void InvalidateRowsMeasure()
        {
            foreach (UIElement element in base.Children)
            {
                DataGridRowPresenter presenter = element as DataGridRowPresenter;
                if (presenter != null)
                {
                    presenter.InvalidateCellsMeasure();
                }
                else
                {
                    DataGridDetailsPresenter presenter2 = element as DataGridDetailsPresenter;
                    if (presenter2 != null)
                    {
                        presenter2.InvalidateMeasure();
                    }
                }
            }
        }

        private void MeasureDetail(ViewRow viewRow)
        {
            if ((viewRow.Row.ActualDetailsVisibility == Visibility.Visible) && (this.View.DataGrid.RowDetailsTemplate != null))
            {
                DataGridDetailsPresenter detailsPresenter = viewRow.GetDetailsPresenter(true);
                if (!base.Children.Contains(detailsPresenter))
                {
                    base.Children.Add(detailsPresenter);
                }
                detailsPresenter.Measure(new Size(double.IsInfinity(this.View.ScrollManager.AvailableCellsAreaWidth) ? this.View.ScrollManager.ExtentWidth : this.View.ScrollManager.AvailableCellsAreaWidth, viewRow.DetailsMeasureHeight));
                viewRow.DetailsDesiredHeight = detailsPresenter.DesiredSize.Height;
            }
            else
            {
                DataGridDetailsPresenter presenter2 = viewRow.GetDetailsPresenter(false);
                if ((presenter2 != null) && base.Children.Contains(presenter2))
                {
                    base.Children.Remove(presenter2);
                }
            }
        }

        protected override Size MeasureOverride(Size availableSize)
        {
            double rowOffset;
            this.View.ClearMouseOver();
            this.View.ScrollManager.AvailableSize = availableSize;
            do
            {
                rowOffset = this.ValidFillParameters.RowOffset;
                this.MaxRowWidth = 0.0;
                if (!double.IsInfinity(availableSize.Height))
                {
                    int num2;
                    int num3;
                    this.View.ScrollManager.GetCurrentVerticalBounds(availableSize.Height, out num2, out num3);
                    this.RemoveUnusedChildren(num2, num3, false);
                }
                this.VisibleRows.ClearVisibleItems();
                foreach (ViewRow row in this.View.GetFrozenAndScrollingRows(this.ValidFillParameters.RowIndex, this.ValidFillParameters.VerticalDirection))
                {
                    if (availableSize.Height == 0.0)
                    {
                        break;
                    }
                    this.VisibleRows.AddVisibleItem(row.Row);
                    foreach (RowPart part in this.GetRowPartsInOrder(row, this.ValidFillParameters.VerticalDirection))
                    {
                        if (part == RowPart.Detail)
                        {
                            this.MeasureDetail(row);
                            rowOffset += row.DetailsRenderHeight;
                        }
                        else
                        {
                            this.MeasureRow(row);
                            rowOffset += row.RenderHeight;
                        }
                    }
                    if (rowOffset >= availableSize.Height)
                    {
                        break;
                    }
                }
            }
            while (this.VisibleRows.HasItems && (!this.CheckViewPortIsFullHorizontally(this.MaxRowWidth) || !this.CheckViewPortIsFullVertically(rowOffset)));
            this.RemoveUnusedChildren(this.VisibleRows.FirstVisibleItem, this.VisibleRows.LastVisibleItem, true);
            if (this.View.RowsHeaderPanel != null)
            {
                this.View.RowsHeaderPanel.InvalidateMeasure();
            }
            double num4 = (this.View.ScrollManager.ExtentWidth + this.View.GetRenderLeadingColumnWidth()) + this.View.GetFrozenWidth(false);
            return new Size((num4 < availableSize.Width) ? Math.Max(0.0, num4) : availableSize.Width, (rowOffset < availableSize.Height) ? Math.Max(0.0, rowOffset) : availableSize.Height);
        }

        private void MeasureRow(ViewRow viewRow)
        {
            DataGridRowPresenter rowPresenter = viewRow.GetRowPresenter(true);
            rowPresenter.MinHeight = viewRow.MinHeight;
            rowPresenter.Measure(new Size(this.View.ScrollManager.AvailableSize.Width, viewRow.MeasureHeight));
            this.MaxRowWidth = Math.Max(this.MaxRowWidth, rowPresenter.DesiredSize.Width);
            if (rowPresenter.CellsPanel != null)
            {
                int maxHeightColumnDisplayIndex = viewRow.MaxHeightColumnDisplayIndex;
                if (((rowPresenter.DesiredSize.Height > viewRow.DesiredHeight) || (maxHeightColumnDisplayIndex < this.View.Model.FrozenColumnCount)) || ((maxHeightColumnDisplayIndex >= this.VisibleColumns.FirstVisibleItem) && (maxHeightColumnDisplayIndex <= this.VisibleColumns.LastVisibleItem)))
                {
                    viewRow.MaxHeightColumnDisplayIndex = rowPresenter.CellsPanel.MaxHeightColumnDisplayIndex;
                    viewRow.DesiredHeight = rowPresenter.DesiredSize.Height;
                }
            }
            else
            {
                viewRow.MaxHeightColumnDisplayIndex = -1;
                viewRow.DesiredHeight = rowPresenter.DesiredSize.Height;
            }
        }

        private void OnSizeChanged(object sender, SizeChangedEventArgs e)
        {
            Size newSize = e.NewSize;
            RectangleGeometry geometry = new RectangleGeometry
            {
                Rect = new Rect(0.0, 0.0, newSize.Width, newSize.Height)
            };
            base.Clip = geometry;
        }

        internal void RecalculateColumnsWidths()
        {
            double num2 = this.View.ScrollManager.CellsAreaWidth - this.View.Model.VerticalFreezingSeparatorWidth;
            List<ViewColumn> list = new List<ViewColumn>();
            double num3 = 0.0;
            foreach (DataGridColumn column in this.GetShownColumns(this.VisibleColumns.FirstVisibleItem, this.VisibleColumns.LastVisibleItem, true))
            {
                ViewColumn item = column.ViewColumn;
                if ((item.Width.Type == DataGridUnitType.Star) || (item.Width.Type == DataGridUnitType.AutoStar))
                {
                    num3 += item.Width.Value;
                    list.Add(item);
                }
                else
                {
                    num2 -= item.RenderWidth;
                }
            }
            foreach (ViewColumn column3 in from viewColumn in list
                                           orderby viewColumn.MinRenderWidth * viewColumn.Width.Value descending
                                           select viewColumn)
            {
                double minRenderWidth = column3.MinRenderWidth;
                if (num2 > 0.0)
                {
                    double num5 = Math.Round((double)((num2 * column3.Width.Value) / num3), 0);
                    minRenderWidth = Math.Max(minRenderWidth, ((column3.Width.Type == DataGridUnitType.AutoStar) && (num5 < column3.DesiredWidth)) ? column3.DesiredWidth : num5);
                }
                column3.RenderWidth = minRenderWidth;
                num2 -= column3.RenderWidth;
                num3 -= column3.Width.Value;
            }
        }

        private void RemoveUnusedChildren(int firstVisibleRowIndex, int lastVisibleRowIndex, bool measureCollectedChildren)
        {
            foreach (UIElement element in new List<UIElement>(base.Children.GetTypedUIElementCollectionWrapper()))
            {
                if (element is DataGridRowPresenter)
                {
                    DataGridRowPresenter child = (DataGridRowPresenter)element;
                    if (!child.IsCollected)
                    {
                        bool flag = false;
                        if (child.ViewRow == null)
                        {
                            flag = true;
                        }
                        else
                        {
                            ViewRow viewRow = child.ViewRow;
                            DataGridRow row = viewRow.Row;
                            int index = row.Index;
                            if ((((index < firstVisibleRowIndex) || (index > lastVisibleRowIndex)) && !viewRow.Row.IsFrozen) || !row.IsVisible(false))
                            {
                                flag = true;
                            }
                        }
                        if (flag)
                        {
                            this.RemoveChild(child);
                        }
                    }
                    else if (measureCollectedChildren)
                    {
                        child.Measure(new Size(0.0, 0.0));
                    }
                }
                else if (element is DataGridDetailsPresenter)
                {
                    DataGridDetailsPresenter presenter2 = (DataGridDetailsPresenter)element;
                    DataGridRow row3 = presenter2.ViewRow.Row;
                    if ((presenter2 != null) && ((((row3.Index < firstVisibleRowIndex) || (row3.Index > lastVisibleRowIndex)) && !row3.IsFrozen) || !row3.IsVisible(false)))
                    {
                        base.Children.Remove(presenter2);
                    }
                }
            }
        }

        private void SetCustomDefaultValues()
        {
            base.SizeChanged += new SizeChangedEventHandler(this.OnSizeChanged);
        }

        internal void UpdateHorizontalOffset()
        {
            double num5;
            double columnOffset = this.ValidFillParameters.ColumnOffset;
            double renderLeadingColumnWidth = this.View.GetRenderLeadingColumnWidth();
            if ((this.ValidFillParameters.HorizontalDirection == GeneratorDirection.Backward) && (this.ValidFillParameters.ColumnOffset == 0.0))
            {
                DataGridColumn column = this.View.Model.Columns.GetVisibleColumnsFromIndex(this.ValidFillParameters.ColumnIndex, this.ValidFillParameters.HorizontalDirection).FirstOrDefault<DataGridColumn>();
                if (column != null)
                {
                    ViewColumn viewColumn = column.ViewColumn;
                    if (((viewColumn.RenderWidth + renderLeadingColumnWidth) + this.View.GetFrozenWidth(false)) > this.View.ScrollManager.AvailableSize.Width)
                    {
                        columnOffset = this.View.ScrollManager.AvailableSize.Width - ((viewColumn.RenderWidth + renderLeadingColumnWidth) + this.View.GetFrozenWidth(false));
                    }
                }
            }
            double num3 = 0.0;
            double num4 = columnOffset + this.View.Model.VerticalFreezingSeparatorWidth;
            if (this.ValidFillParameters.HorizontalDirection == GeneratorDirection.Forward)
            {
                num5 = columnOffset;
            }
            else
            {
                double num6 = this.View.ScrollManager.ExtentWidth + this.View.GetFrozenWidth(false);
                double num7 = this.View.ScrollManager.AvailableSize.Width - renderLeadingColumnWidth;
                num5 = (num6 < this.View.ScrollManager.AvailableSize.Width) ? Math.Max(0.0, num6) : (num7 - columnOffset);
            }
            foreach (ViewColumn column3 in this.View.GetFrozenAndScrollingColumns(this.ValidFillParameters.ColumnIndex, this.ValidFillParameters.HorizontalDirection))
            {
                if (column3.Column.IsFrozen)
                {
                    column3.Offset = num3;
                    num3 += column3.RenderWidth;
                    num4 += column3.RenderWidth;
                }
                else if (this.ValidFillParameters.HorizontalDirection == GeneratorDirection.Forward)
                {
                    column3.Offset = (num3 + this.View.Model.VerticalFreezingSeparatorWidth) + num5;
                    num5 += column3.RenderWidth;
                    num4 += column3.RenderWidth;
                }
                else
                {
                    num5 -= column3.RenderWidth;
                    column3.Offset = num5;
                    num4 += column3.RenderWidth;
                }
                if (num4 >= this.View.ScrollManager.Viewport.Width)
                {
                    break;
                }
            }
            this.View.ScrollManager.FrozenLeftWidth = num3;
            this.View.ScrollManager.FrozenRightWidth = 0.0;
            if (this.VisibleColumns.FirstVisibleItem >= 0)
            {
                if (Math.Round(this.GetRenderWidthOfShownColumns(this.VisibleColumns.FirstVisibleItem, this.VisibleColumns.LastVisibleItem, false), 0) >= Math.Round(this.View.ScrollManager.ScrollingWidth, 0))
                {
                    int columnIndex = this.ValidFillParameters.ColumnIndex;
                    double num10 = columnOffset;
                    if (this.ValidFillParameters.HorizontalDirection == GeneratorDirection.Forward)
                    {
                        this.View.ScrollManager.HorizontalOffset = this.View.ScrollManager.GetScrollPositionFromColumnDisplayIndex(columnIndex) - num10;
                    }
                    else
                    {
                        this.View.ScrollManager.HorizontalOffset = (this.View.ScrollManager.GetScrollPositionFromColumnDisplayIndex(columnIndex + 1) + -this.View.ScrollManager.ScrollingWidth) + num10;
                    }
                }
                else
                {
                    this.View.ScrollManager.HorizontalOffset = 0.0;
                }
            }
        }

        private void UpdateRowDetailsOffsets(ref double topRowOffset, ref double bottomRowOffset, ref double scrollingRowOffset, ViewRow viewRow)
        {
            if (viewRow.Row.IsTopFrozen)
            {
                viewRow.DetailsOffset = topRowOffset;
                topRowOffset += viewRow.DetailsRenderHeight;
            }
            else if (viewRow.Row.IsBottomFrozen)
            {
                viewRow.DetailsOffset = (this.View.ScrollManager.Viewport.Height - bottomRowOffset) - viewRow.DetailsRenderHeight;
                bottomRowOffset += viewRow.DetailsRenderHeight;
            }
            else if (this.ValidFillParameters.VerticalDirection == GeneratorDirection.Forward)
            {
                viewRow.DetailsOffset = topRowOffset + scrollingRowOffset;
                scrollingRowOffset += viewRow.DetailsRenderHeight;
            }
            else
            {
                double num = Math.Min(this.View.ScrollManager.ScrollingHeight, this.View.ScrollManager.ExtentHeight);
                viewRow.DetailsOffset = ((topRowOffset + num) - scrollingRowOffset) - viewRow.DetailsRenderHeight;
                scrollingRowOffset += viewRow.DetailsRenderHeight;
            }
        }

        private void UpdateRowOffsets(ref double topRowOffset, ref double bottomRowOffset, ref double scrollingRowOffset, ViewRow viewRow)
        {
            if (viewRow.Row.IsTopFrozen)
            {
                viewRow.Offset = topRowOffset;
                topRowOffset += viewRow.RenderHeight;
            }
            else if (viewRow.Row.IsBottomFrozen)
            {
                viewRow.Offset = (this.View.ScrollManager.Viewport.Height - bottomRowOffset) - viewRow.RenderHeight;
                bottomRowOffset += viewRow.RenderHeight;
            }
            else if (this.ValidFillParameters.VerticalDirection == GeneratorDirection.Forward)
            {
                viewRow.Offset = topRowOffset + scrollingRowOffset;
                scrollingRowOffset += viewRow.RenderHeight;
            }
            else
            {
                double num = Math.Min(this.View.ScrollManager.ScrollingHeight, this.View.ScrollManager.ExtentHeight);
                viewRow.Offset = ((topRowOffset + num) - scrollingRowOffset) - viewRow.RenderHeight;
                scrollingRowOffset += viewRow.RenderHeight;
            }
        }

        internal void UpdateVerticalOffset()
        {
            int rowIndex = this.ValidFillParameters.RowIndex;
            double rowOffset = this.ValidFillParameters.RowOffset;
            if (this.ValidFillParameters.VerticalDirection == GeneratorDirection.Forward)
            {
                this.View.ScrollManager.VerticalOffset = this.View.ScrollManager.GetScrollPositionFromRowIndex(rowIndex) - rowOffset;
            }
            else
            {
                this.View.ScrollManager.VerticalOffset = Math.Max((double)0.0, (double)((this.View.ScrollManager.GetScrollPositionFromRowIndex(rowIndex + 1) - this.View.ScrollManager.ScrollingHeight) + rowOffset));
            }
            double topRowOffset = 0.0;
            double bottomRowOffset = 0.0;
            double scrollingRowOffset = this.ValidFillParameters.RowOffset;
            double num6 = this.ValidFillParameters.RowOffset;
            foreach (ViewRow row in this.View.GetFrozenAndScrollingRows(this.ValidFillParameters.RowIndex, this.ValidFillParameters.VerticalDirection))
            {
                foreach (RowPart part in this.GetRowPartsInOrder(row, this.ValidFillParameters.VerticalDirection))
                {
                    if (part == RowPart.Detail)
                    {
                        this.UpdateRowDetailsOffsets(ref topRowOffset, ref bottomRowOffset, ref scrollingRowOffset, row);
                        num6 += row.DetailsRenderHeight;
                    }
                    else
                    {
                        this.UpdateRowOffsets(ref topRowOffset, ref bottomRowOffset, ref scrollingRowOffset, row);
                        num6 += row.RenderHeight;
                    }
                }
                row.Row.ActualHeight = row.GetTotalRenderHeight();
                if (num6 >= this.View.ScrollManager.Viewport.Height)
                {
                    break;
                }
            }
            this.View.ScrollManager.FrozenTopHeight = topRowOffset;
            this.View.ScrollManager.FrozenBottomHeight = bottomRowOffset;
        }

        // Properties
        private FillParameters ValidFillParameters
        {
            get
            {
                return this.View.ScrollManager.ValidFillParameters;
            }
        }

        internal DataGridView View { get; set; }

        private DataGridVisibleItems<DataGridColumn> VisibleColumns
        {
            get
            {
                return this.View.Viewport.VisibleColumns;
            }
        }

        internal DataGridVisibleItems<DataGridRow> VisibleRows
        {
            get
            {
                return this.View.Viewport.VisibleRows;
            }
        }

        private enum RowPart
        {
            Row,
            Detail
        }
    }
}
